package com.android.tiango.baba.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.android.scsd.tiango.R;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.controller.listener.SocializeListeners.UMAuthListener;
import com.umeng.socialize.exception.SocializeException;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.SmsHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

/**
 * 友盟、社会化分享公共类
 * 
 * @author txg
 * 
 */
public class SocialUtils {
	public static UMSocialService mController;
	public UMSocialService mController_Login;
	public ShareListener listener;

	public SocialUtils() {
		if (mController == null) {
			mController = UMServiceFactory.getUMSocialService(Constants.UMENG_SHARE);
		}
		mController_Login = UMServiceFactory.getUMSocialService(Constants.UMENG_SHARE_LOGIN);
	}

	/**
	 * 设置微信内容
	 * 
	 * @param context
	 */

	public void setShareWeiXin(final Context context) {
		// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
		// 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(context, Constants.WEIXIN_APPID, Constants.WEIXIN_APPSECRET);
		wxHandler.addToSocialSDK();
		// 设置微信好友分享内容
		WeiXinShareContent weixinContent = new WeiXinShareContent();
		// 设置分享文字
		weixinContent.setShareContent("来自友盟社会化组件（SDK）让移动应用快速整合社交分享功能，微信");
		// 设置title
		weixinContent.setTitle("友盟社会化分享组件-微信");
		// 设置分享内容跳转URL
		weixinContent.setTargetUrl("你的URL链接(设置)");
		// 设置分享图片
		weixinContent.setShareImage(new UMImage(context, R.drawable.icon_tiangou));
		mController.setShareMedia(weixinContent);
		SHARE_MEDIA media = SHARE_MEDIA.WEIXIN;
		postShare(context, media);
	}

	public ShareListener getListener() {
		return listener;
	}

	public void setListener(ShareListener listener) {
		this.listener = listener;
	}

	/**
	 * 进行分享
	 * 
	 * @param media
	 *        类别
	 */
	private void postShare(final Context context, SHARE_MEDIA media) {
		mController.postShare(context, media, new SnsPostListener() {
			public void onComplete(SHARE_MEDIA platform, int eCode, SocializeEntity entity) {
				if (eCode == 200) {
					Toast.makeText(context, "分享成功.", Toast.LENGTH_SHORT).show();
					if (null != listener) {
						listener.onShareOk();
					}
				} else {
					String eMsg = "";
					if (eCode == -101) {
						eMsg = "没有授权";
					}
					Toast.makeText(context, "分享失败[" + eCode + "] " + eMsg, Toast.LENGTH_SHORT) .show();
					if (null != listener) {
						listener.onShareFail("分享失败[" + eCode + "] " + eMsg);
					}
				}
			}

			public void onStart() {
			}
		});
	}

	public interface ShareListener {
		void onShareOk();

		void onShareFail(String msg);
	}

	/**
	 * 设置朋友圈内容
	 */
	public void setShareWeiXin_Circle(final Context context, String content, String tarUrl,
			String title) {
		setShareWeiXin_Circle(context, content, tarUrl, title, null);
	}

	public void setShareWeiXin_Circle(final Context context, String content, String tarUrl,
			String title, Bitmap bmp) {
		// 支持微信朋友圈
		UMWXHandler wxCircleHandler = new UMWXHandler(context, Constants.WEIXIN_APPID, Constants.WEIXIN_APPSECRET);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();
		// 设置微信朋友圈分享内容
		CircleShareContent circleMedia = new CircleShareContent();
//		circleMedia.setShareContent(content);
		// 设置朋友圈title
		circleMedia.setTitle(title);
		if (null == bmp) {
			circleMedia.setShareImage(new UMImage(context, R.drawable.icon_tiangou));
		} else {
			circleMedia.setShareImage(new UMImage(context, bmp));

		}
		circleMedia.setTargetUrl(tarUrl);
		mController.setShareMedia(circleMedia);
		postShare(context, SHARE_MEDIA.WEIXIN_CIRCLE);
	}

	/**
	 * 设置QQ内容
	 * 
	 * @param context
	 */
	public void setShareQQ(Activity activity) {
		// 参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
		UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(activity, Constants.QQ_APPID,
				Constants.QQ_APPKEY);
		qqSsoHandler.addToSocialSDK();

		QQShareContent qqShareContent = new QQShareContent();
		// 设置分享文字
		qqShareContent.setShareContent("来自友盟社会化组件（SDK）让移动应用快速整合社交分享功能 -- QQ");
		// 设置分享title
		qqShareContent.setTitle("hello, title");
		// 设置分享图片
		qqShareContent.setShareImage(new UMImage(activity, R.drawable.icon_tiangou));
		// 设置点击分享内容的跳转链接
		qqShareContent.setTargetUrl("你的URL链接");
		mController.setShareMedia(qqShareContent);
		postShare(activity, SHARE_MEDIA.QQ);

	}

	/**
	 * 设置QQ空间内容并进行分享
	 * 
	 * @param activity
	 * @param content
	 *        内容
	 * @param tarUrl
	 *        链接
	 * @param title
	 *        标题
	 */
	
	public void setShareQQZoe(Activity activity, String content, String tarUrl, String title) {
		setShareQQZoe(activity, content, tarUrl, title, null);
	}
	public void setShareQQZoe(Activity activity, String content, String tarUrl, String title,
			Bitmap bmp) {
		QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(activity, Constants.QQ_APPID,
				Constants.QQ_APPKEY);
		qZoneSsoHandler.addToSocialSDK();

		QZoneShareContent qzone = new QZoneShareContent();
		if (content != null) {
			// 设置分享文字
			qzone.setShareContent(content);
		}
		// 设置点击消息的跳转URL
		qzone.setTargetUrl(tarUrl);
		// 设置分享内容的标题
		qzone.setTitle(title);
		// 设置分享图片
//		if (null == bmp) {
//			qzone.setShareImage(new UMImage(activity, R.drawable.bg_shareg_getthe));
//		} else {
//			qzone.setShareImage(new UMImage(activity, bmp));
//		}
		mController.setAppWebSite("1111111111  www.baid.com");
		mController.setShareContent("测试");
		mController.setShareMedia(new UMImage(activity, R.drawable.bg_shareg_getthe));
		
//		mController.setShareMedia(qzone);
		postShare(activity, SHARE_MEDIA.QZONE);
	}

	/**
	 * 设置新浪内容并进行分享
	 * 
	 * @param context
	 * @param content
	 *        内容
	 * @param tarUrl
	 *        点击URL
	 * @param title
	 *        标题
	 */

	public void setShareSina(Context context, String content, String tarUrl, String title) {
		setShareSina(context, content, tarUrl, title, null);
	}
	public void setShareSina(Context context, String content, String tarUrl, String title,
			Bitmap bmp) {
		// 设置新浪SSO
		mController.getConfig().setSsoHandler(new SinaSsoHandler());
		SinaShareContent sina = new SinaShareContent();
		// 设置分享文字
//		sina.setShareContent(content);
		// 设置点击消息的跳转URL
		sina.setTargetUrl(tarUrl);
		// 设置分享内容的标题
		sina.setTitle(title);
		// 设置分享图片
		if (null == bmp) {
			sina.setShareImage(new UMImage(context, R.drawable.bg_shareg_getthe));
		} else {
			sina.setShareImage(new UMImage(context, bmp));

		}
		mController.setShareMedia(sina);
		postShare(context, SHARE_MEDIA.SINA);
	}

	/**
	 * 设置短信内容
	 * 
	 * @param context
	 */
	public void setShareSms(Context context, String text) {
		SmsHandler smsHandler = new SmsHandler();
		smsHandler.addToSocialSDK();
		mController.setShareContent(text);
		postShare(context, SHARE_MEDIA.SMS);
	}

	/**
	 * 第三方登陆
	 * @param context
	 * @param media
	 */
	public void setOtherLogin(final Context context, SHARE_MEDIA media) {
		mController_Login.getConfig().setSsoHandler(new SinaSsoHandler());
		mController_Login.doOauthVerify(context, media, new UMAuthListener() {
			@Override
			public void onError(SocializeException e, SHARE_MEDIA platform) {
				System.out.println("error:"+e.getMessage());
			}

			@Override
			public void onComplete(Bundle value, SHARE_MEDIA platform) {
				if (value != null && !TextUtils.isEmpty(value.getString("uid"))) {
					Toast.makeText(context, "授权成功.", Toast.LENGTH_SHORT).show();
					// 获取accesstoken及用户资料
					// mController_Login.
				} else {
					Toast.makeText(context, "授权失败", Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onCancel(SHARE_MEDIA platform) {
				System.out.println("取消");
			}

			@Override
			public void onStart(SHARE_MEDIA platform) {
				System.out.println("开始");
			}
		});
	}

}
